package cn.tedu.mall.seckill.timer.config;

import cn.tedu.mall.seckill.timer.job.SeckillBloomInitialJob;
import cn.tedu.mall.seckill.timer.job.SeckillInitialJob;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class QuartzConfig {
    //定义JobDetail
    @Bean
    public JobDetail initJobDetail(){
        log.info("预热任务绑定");
        return JobBuilder.newJob(SeckillInitialJob.class)
                .withIdentity("initialJob")
                .storeDurably()
                .build();
    }
    //定义触发器Trigger
    @Bean
    public Trigger initSeckillTrigger(){
        log.info("预热触发器运行");
        //cron表达式，为了学习，设置成每分钟运行一次
        CronScheduleBuilder cron = CronScheduleBuilder.cronSchedule("0 0/1 * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(initJobDetail())
                .withIdentity("initialTrigger")
                .withSchedule(cron)
                .build();
    }
    //加载布隆过滤器的定义
    @Bean
    public JobDetail seckillBloomJobDetail(){
        log.info("加载布隆过滤器");
        return JobBuilder.newJob(SeckillBloomInitialJob.class)
                .withIdentity("seckillBloom")
                .storeDurably()
                .build();
    }
    @Bean
    public Trigger seckillBloomTrigger(){
        log.info("布隆过滤器触发器运行");
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule("0 0/1 * * * ?");
        return TriggerBuilder.newTrigger()
                .forJob(seckillBloomJobDetail())
                .withIdentity("seckillBloomTrigger")
                .withSchedule(cronScheduleBuilder)
                .build();
    }
}
